package com.mobilad.validations;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.mobilad.dao.UserDAO;
import com.mobilad.service.*;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

@FacesValidator("phoneValidation")
public class PhoneValidator implements Validator {

		private Matcher matcher;
	    Pattern pattern = Pattern.compile("\\d{8}");
	    private UserService userService;
	    
    
	    public UserService getUserService() {
			return userService;
		}


		public void setUserService(UserService userService) {
			this.userService = userService;
		}


		@Override
	  	public void validate(FacesContext context, UIComponent component,
	  			Object value) throws ValidatorException {		
	    	
			matcher = pattern.matcher(value.toString());
			if(!matcher.matches()){
	 
				FacesMessage msg = 
					new FacesMessage("Phone validation failed.", 
							"Invalid phone number.");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
	 
			}
		}
	
}
